package com.siyue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siyue.entity.Active;
import com.siyue.entity.UserAction;
import com.siyue.mapper.UserActionMapper;
import com.siyue.service.ActiveService;
import com.siyue.service.UserActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小沐沐吖
 * @since 2023-07-16
 */
@Service
public class UserActionServiceImp extends ServiceImpl<UserActionMapper, UserAction> implements UserActionService {

    @Autowired
    private ActiveService activeService;

    @Override
    @Transactional
    public String praise(Integer userId, Integer activeId) throws InterruptedException {
        UserAction userAction = null;
        userAction = this.getOne(new LambdaQueryWrapper<UserAction>().eq(UserAction::getUserId, userId)
                .eq(UserAction::getArticleId, activeId)
        );
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName());
        Active active = activeService.getById(activeId);
        if (BeanUtil.isEmpty(userAction)) {
            //点赞加一
            active.setPraiseNum(active.getPraiseNum() + 1);
            userAction = new UserAction();
            userAction.setArticleId(activeId.toString());
            userAction.setUserId(userId.toString());
            this.save(userAction);
        } else {
            //点赞减一
            active.setPraiseNum(active.getPraiseNum() - 1);
            this.removeById(userAction.getId());
        }

        activeService.updateById(active);
        return userAction.toString();
    }

    @Override
    public String praise1(Integer userId, Integer activeId) throws InterruptedException {
        UserAction userAction = null;
        userAction = this.getOne(new LambdaQueryWrapper<UserAction>().eq(UserAction::getUserId, userId)
                .eq(UserAction::getArticleId, activeId)
        );
        Thread.sleep(4000);
        System.out.println(Thread.currentThread().getName());
        Active active = activeService.getById(activeId);
        if (BeanUtil.isEmpty(userAction)) {
            //点赞加一
            active.setPraiseNum(active.getPraiseNum() + 1);
            userAction = new UserAction();
            userAction.setArticleId(activeId.toString());
            userAction.setUserId(userId.toString());
            this.save(userAction);
        } else {
            //点赞减一
            active.setPraiseNum(active.getPraiseNum() - 1);
            this.removeById(userAction.getId());
        }
        activeService.updateById(active);
        return userAction.toString();
    }


    /**
     * 加锁通过sql   调用存储过程
     *
     * @param userId
     * @param activeId
     * @return
     */
    @Override
    public String praiseLockWithSQL(Integer userId, Integer activeId) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("userId", userId);
        m.put("articleId", activeId);
        baseMapper.praiseLockWithSQL(m);
        Long resultCount = (Long) m.get("resultCount");
        return resultCount.toString();
    }

}
